package com.gitee.ywj1352.算法.week02;

/**
 * 归并
 */
public class 翻转二叉树 {


    public static TreeNode invertTree(TreeNode root) {
        if (root == null) return null;
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        TreeNode left = invertTree(root.left);
        TreeNode right = invertTree(root.right);
        root.right = left;
        root.left = right;
        return root;
    }


    public static void main(String[] args) {
        TreeNode t1 = new TreeNode(1);
        TreeNode t2 = new TreeNode(2);
        TreeNode t3 = new TreeNode(3);
        TreeNode t4 = new TreeNode(4);
        TreeNode t5 = new TreeNode(5);
        TreeNode t6 = new TreeNode(6);
        TreeNode t7 = new TreeNode(7);
        TreeNode t8 = new TreeNode(8);
        t5.left = t3;
        t5.right = t8;
        t3.left = t2;
        t3.right = t4;
        t2.left = t1;
        t8.left = t6;
        t8.right = t7;


        TreeNode treeNode = invertTree(t5);


        System.out.println(treeNode);

    }
}
